/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.mushroom.website.rest;

//
import cz.muni.fi.pa165.mushroom.website.dto.ForestDto;
import cz.muni.fi.pa165.mushroom.website.dto.MushroomDto;
import cz.muni.fi.pa165.mushroom.website.servicesImpl.ForestServiceImpl;
import java.net.URI;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller for REST forest API
 *
 * @author Roman Jakubco <roman.jakubco@gmail.com>
 */
@Path("forest")
public class ForestRESTController {

    @Autowired
    private ForestServiceImpl forestServiceImpl;
    @Context
    private UriInfo context;

    @GET
    @Path("listForests")
    @Produces(MediaType.APPLICATION_JSON)
    public List<ForestDto> listForests() {
        List<ForestDto> fds = forestServiceImpl.listForests();
        return fds;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("createForest")
    public Response createForest(ForestDto forestDto) {
        forestServiceImpl.createForest(forestDto);
        System.out.println("Created forest " + forestDto.getId());
        return Response.created(URI.create(context.getAbsolutePath() + "/" + forestDto.getId())).build();
    }

    @GET
    @Path("getForest/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public ForestDto getForest(@PathParam("id") Long id) {
        try {
            return forestServiceImpl.findForest(id);
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException();
        }
    }

    @GET
    @Path("getMushrooms/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<MushroomDto> getMushrooms(@PathParam("id") Long id) {
        return forestServiceImpl.listFoundMushrooms(forestServiceImpl.findForest(id));
    }

    @PUT
    @Path("editForest")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response editForest(ForestDto forestDto) {
        forestServiceImpl.updateForest(forestDto);
        System.out.println("Updated Forest: " + forestDto);
        return Response.created(URI.create(context.getAbsolutePath() + "/" + forestDto.getId())).build();
    }

    @DELETE
    @Path("deleteForest/{id}")
    public Response deleteForest(@PathParam("id") Long id) {
        ForestDto fd = forestServiceImpl.findForest(id);
        forestServiceImpl.removeForest(fd);
        System.out.println("Deleted Forest: " + fd);
        return Response.created(URI.create(context.getAbsolutePath() + "/" + fd)).build();
    }
    
}
